<template>
  <div class="ranking">
    <h3 class="ranking__section-title">实时AQI城市排名</h3>
    <h2 class="ranking__title">主要城市空气质量排名</h2>
    <el-table :data="tableData" style="width: 100%; margin-bottom: 12px">
      <el-table-column prop="id" label="#" width="40" align="center"></el-table-column>
      <el-table-column label="城市" width="130" align="center">
        <template slot-scope="scope">
          <div @click="toIdDetail(scope.row.cityId)">
            <el-link type="primary">{{ scope.row.city }}</el-link>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="AQI" label="AQI" width="40" align="center"></el-table-column>
      <el-table-column label="空气等级" width="100" align="center">
        <template slot-scope="scope">
          <div :style="scope.row.backgroundColor">
            <span style="color: white;font-weight: bold;">{{ scope.row.airType }}</span>
          </div>
        </template>
      </el-table-column>
    </el-table>
    <p class="ranking__last-update-time">{{ currentTime }}(当前时间)</p>
    <div class="iqair-contained-button">
      <el-button type="primary" @click="toAllRank()">查看所有城市AQI排名</el-button>
    </div>
  </div>
</template>

<script>
import {mapState} from "vuex";

export default {
  data() {
    return {
      tableData: [],
      currentTime: "",
    };
  },
  mounted() {
    this.getNowDate();
    this.getShowAir();
  },
  methods: {

    //跳转到详细信息
    async toIdDetail(cityId) {
      await this.$store.dispatch("detail/getIdCityList", cityId);
      this.$router.push("/detail/" + cityId);
    },

    //查看全部排序
    toAllRank() {
      this.$router.push("/rank")
    },
    // 获取当前时间
    getNowDate() {
      let year = new Date().getFullYear(); //获取当前时间的年份
      let month = new Date().getMonth() + 1; //获取当前时间的月份
      let day = new Date().getDate(); //获取当前时间的天数
      this.currentTime = " " + year + "年" + month + "月" + day + "日";
    },

    // 得到空气数据
    async getShowAir() {
      let a = 1;
      await this.$store.dispatch("rank/getshowAirLimit");
      // console.log(this.airInfo);
      this.airInfo.map((data) => {
        this.tableData.push({
          airType: data.airTypeId,
          AQI: data.aqi,
          city: data.cityName,
          cityId: data.cityId,
          id: a++,
          backgroundColor: "background-color: " + data.color
        });
      });
    },
  },
  computed: {
    //获取空气排行信息
    ...mapState("rank", {airInfo: "airInfoLimit"}),
  },
};
</script>

<style lang="less" scoped>
.ranking {
  padding: 24px;
  margin-bottom: 24px;
  border-radius: 4px;
  box-shadow: 0 2px 20px 0 rgba(0, 0, 0, 0.08);
  height: inherit;
  background-color: #fff;

  .ranking__section-title {
    margin-bottom: 8px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 12px;
    line-height: 1.5;
    letter-spacing: 0.9px;
    text-transform: uppercase;
    color: #499fbc;
  }

  .ranking__title {
    margin-bottom: 24px;
    font-family: Arial, Helvetica, sans-serif;
    font-size: 18px;
    line-height: 1.44;
    color: #414141;
    font-weight: 500;
  }

  .ranking__last-update-time {
    width: 100%;
    text-align: center;
    font-size: 12px;
    line-height: 1.5;
    color: #5b7186;
    margin-bottom: 16px;
  }

  .iqair-contained-button {
    text-align: center;
  }

  .el-button--primary {
    width: 100%;
  }
}
</style>